var express = require('express');
var router = express.Router();
var query = require('../my_modules/sql');
var moment = require('moment');

function cdate(results) {
    for (var i=0; i<results.length; i++) {
        results[i].startdate =  moment(results[i].startdate).format('YYYY-MM-DD');
        results[i].iccid =  moment(results[i].iccid).format('YYYY-MM-DD HH:mm:ss');
    }
}

router.get('/', function (req, res) {
    console.log("check login");
    if (!req.session.user) {
        console.log("user" + req.session.user);
        res.json({status: 'fail', msg: '请登录'});
    }else{
        var name = req.session.user;
        var sql = "select * from tickets where name = '" + name + "'";
        console.log(sql);
        query(sql, function (qerr,result,fields) {
            if(result.length == 0){
                res.json({msg:'没有数据喔~~，去预定票务吧~~',status:'fail'});
            }else{
                console.log(result);
                cdate(result);
                res.json({msg:'有了有了', tickets: result,status:'ok'});
            }
        });
    }

});

router.post('/create', function (req, res) {
    console.log("check login");
    if (!req.session.user) {
        console.log("user" + req.session.user);
        res.json({status: 'fail', msg: '请登录'});
    } else {
        var name = req.session.user;
        var bookname = req.body.bookname;
        var sex = req.body.sex;
        var startcity = req.body.startcity;
        var endcity = req.body.endcity;
        var startdate = req.body.startdate;
        var idcard = req.body.idcard;
        var sqlsoource = "insert into tickets (name,bookname,booksex,startcity,endcity,startdate, idcard) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}');";
        var sql = sqlsoource.format(name, bookname, sex,  startcity, endcity, startdate, idcard);;
        console.log(sql);
        query(sql, function (qerr, result, fields) {
            if (qerr) {
                res.json({msg: '服务器维护中。。', status: 'fail'});
            } else {
                console.log("插入的tickets" + result.affectedRows);
                if(result.affectedRows > 0) {
                    res.json({status: 'ok', msg: '预定成功'});
                }
                else{
                    res.json({status: 'fail', msg: '预定失败'});
                }

            }
        });
    }
});

router.post('/delete', function (req, res) {
    console.log("check login");
    if (!req.session.user) {
        console.log("user" + req.session.user);
        res.json({status: 'fail', msg: '请登录'});
    } else {
        var iccid = req.body.iccid;
        var sqlsoource = "delete from tickets where iccid = '{0}'";
        var sql = sqlsoource.format(iccid);;
        console.log(sql);
        query(sql, function (qerr, result, fields) {
            if (qerr) {
                res.json({msg: '服务器维护中。。', status: 'fail'});
            } else {
                console.log("删除的tickets" + result.affectedRows);
                if(result.affectedRows > 0) {
                    res.json({status: 'ok', msg: '删除成功'});
                }
                else{
                    res.json({status: 'fail', msg: '删除失败'});
                }

            }
        });
    }
});

router.post('/update', function (req, res) {
    console.log("check login");
    if (!req.session.user) {
        console.log("user" + req.session.user);
        res.json({status: 'fail', msg: '请登录'});
    } else {
        var name = req.session.user;
        var bookname = req.body.bookname;
        var sex = req.body.booksex;
        var startcity = req.body.startcity;
        var endcity = req.body.endcity;
        var startdate = req.body.startdate;
        var idcard = req.body.idcard;
        var iccid = req.body.iccid;
        var sqlsoource = "update tickets set bookname = '{0}', "+
            "booksex = '{1}', " +
            "startcity = '{2}'," +
            "endcity = '{3}', " +
            "startdate = '{4}' ," +
            "idcard = '{5}' " +
            "where iccid = '{6}'";
        var sql = sqlsoource.format( bookname, sex,  startcity, endcity, startdate, idcard, iccid);;
        console.log(sql);
        query(sql, function (qerr, result, fields) {
            if (qerr) {
                res.json({msg: '服务器维护中。。', status: 'fail'});
            } else {
                if(result.affectedRows > 0) {
                    res.json({status: 'ok', msg: '更新成功'});
                }
                else{
                    res.json({status: 'fail', msg: '更新失败'});
                }

            }
        });
    }
});

module.exports = router;